/*
  File:    LogfileEntry.cpp
  Purpose: Utilities for making log file entries.

  Contact: Paul Macklin
           pmacklin@math.uci.edu
		  http://math.uci.edu/~pmacklin
*/

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>
#include <ctime>
#include <cstring>

using namespace std;

#include "LogfileEntry.h"
bool FirstLogEntry;

bool MakeLogEntry( char* LogFile, char* Text )
{
 if( FirstLogEntry )
 {
  return MakeFirstLogEntry( LogFile , Text );
 }
 
 char* TimeString;
 TimeString = new char [256];
 
 time_t rawtime;
 struct tm * timeinfo;
 time( &rawtime );
 timeinfo = localtime ( &rawtime ); 
 sprintf( TimeString, "%s", asctime(timeinfo) );
 TimeString[ strlen(TimeString)-1 ] = '\0';

 ofstream LogStream;
 LogStream.open( LogFile , ios::out|ios::app ); 
 LogStream << "[" << TimeString << "]" << endl
           << Text << endl;
 LogStream.close();
 
 return true;
}

bool InitializeLogfileInternals( void )
{ FirstLogEntry = true; return true; }

bool MakeFirstLogEntry( char* LogFile , char* Text )
{
 if( !FirstLogEntry )
 { return MakeLogEntry( LogFile , Text ); }

 char* TimeString;
 TimeString = new char [256];
 
 time_t rawtime;
 struct tm * timeinfo;
 time( &rawtime );
 timeinfo = localtime ( &rawtime ); 
 sprintf( TimeString, "%s", asctime(timeinfo) );
 TimeString[ strlen(TimeString)-1 ] = '\0';

 ofstream LogStream;
 LogStream.open( LogFile , ios::out ); 
 LogStream << "[" << TimeString << "]" << endl
           << Text << endl;
 LogStream.close();

 FirstLogEntry = false;

 return true;
}

bool StartStringBuffer( char* input )
{
 extern char* StringBuffer;
 StringBuffer[0] = '\0';
 strcpy( StringBuffer, input );
 return true;
}

bool AddToStringBuffer( char* input )
{
 extern char* StringBuffer;
 strcat( StringBuffer, input );
 return true;
}


